package org.windey.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;

/**
 * 
 * 
 *
 */
public class WindeyConnection extends Thread
{
	/**
	 * 
	 */
	private boolean run;
	private Socket socket;
	private boolean linked;
	/**
	 * 
	 */
	private String connectionKey;
	/**
	 * 
	 */
	private BufferedReader in;
	/**
	 * 
	 */
	private PrintWriter out;
	
	/**
	 * 
	 * @param socket
	 */
	public WindeyConnection(Socket socket)
	{
		setLinked(false);
		setRunning(true);
		this.socket = socket;
		try
		{
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param run
	 */
	public void setRunning(boolean run)
	{
		this.run = run;
	}

	/**
	 * 
	 * @return
	 */
	public boolean isRunning() {
		return run;
	}
	
	/**
	 * 
	 * @return
	 */
	public String getConnectionKey()
	{
		return this.connectionKey;
	}
	
	/**
	 * 
	 * @param data
	 */
	public void send(String data)
	{
		out.println(data);
	}
	
	/**
	 * 
	 * @return
	 */
	public String receive()
	{
		String result = "";
		try
		{
			result = in.readLine();
		}
		catch (SocketException e)
		{
			setRunning(false);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 
	 * @param connectionKey
	 */
	public void setConnectionKey(String connectionKey) {
		this.connectionKey = connectionKey;
	}
	
	public Socket getSocket()
	{
		return this.socket;
	}

	public void setLinked(boolean linked)
	{
		this.linked = linked;
	}
	
	public boolean getLinked()
	{
		return linked;
	}

	public void setOffline()
	{
	}
}
